package com.zxy.supplier_system.server.controller

import com.zxy.supplier_system.server.entity.Authority
import com.zxy.supplier_system.server.service.RoleService
import org.springframework.web.bind.annotation.*
import java.security.Principal

@RestController
@RequestMapping("roles")
class RoleController(
    private val roleService: RoleService
) {

    data class AddRoleRequest(
        val name:String
    )

    @HasAnyAuthority(Authority.人员_角色_新增)
    @PostMapping
    fun createRole(@RequestBody addRoleRequest: AddRoleRequest):AddRoleResponse{
        return this.roleService.createRole(addRoleRequest)
    }

    data class AddRoleResponse(
        val id:Int
    )

    data class UpdateRoleNameRequest(
        val name: String
    )

    @HasAnyAuthority(Authority.人员_角色_修改名称)
    @PatchMapping("{name}/name")
    fun updateRoleName(@RequestBody updateRoleNameRequest: UpdateRoleNameRequest,@PathVariable name:String){
        this.roleService.updateRoleName(name,updateRoleNameRequest)
    }

    data class RoleResponse(
        val id:Int,
        val name:String,
        val authorities: MutableList<Authority>,
        val employeeCount: Int
    )

    @HasAnyAuthority(Authority.人员_角色_查询)
    @GetMapping
    fun getRoles(principal: Principal):List<RoleResponse>{
        return this.roleService.getRoles()
    }

    @HasAnyAuthority(Authority.人员_角色_删除)
    @DeleteMapping("{name}")
    fun deleteRole(@PathVariable name: String){
        return this.roleService.deleteRole(name)
    }

    @HasAnyAuthority(Authority.人员_角色_查询)
    @GetMapping("{name}/authorities")
    fun getRoleAuthorities(@PathVariable name: String): MutableList<Authority> {
        return this.roleService.getRoleAuthorities(name)
    }

    @HasAnyAuthority(Authority.人员_角色_设置权限)
    @PatchMapping("{name}/authorities")
    fun updateAuthorities(@PathVariable name:String,@RequestBody authorities:List<Authority>){
        return this.roleService.updateRoleAuthorities(name,authorities)
    }

}